package com.biz.primus.ms.payment.api;

import com.biz.primus.model.payment.vo.req.*;
import com.biz.primus.model.payment.vo.resp.*;
import com.biz.primus.ms.base.api.BaseApiController;
import com.biz.primus.ms.payment.channel.allinpay.res.AllInPayH5PayRespVo;
import com.biz.primus.ms.payment.channel.wechat.exceptions.PaymentException;
import com.biz.primus.ms.payment.channel.wechat.exceptions.SignIncorrectException;
import com.biz.primus.ms.payment.exception.PaymentOrderIsExpiredException;
import com.biz.primus.ms.payment.exception.PaymentOrderIsPaidException;
import com.biz.primus.ms.payment.service.AllInPayPaymentService;
import com.biz.primus.ms.payment.service.PaymentService;
import com.biz.primus.ms.payment.service.SupportPaymentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author defei
 * @date 2017/9/22
 */
@RestController
@RequestMapping("soa/paymentService")
@Api(description = "支付单相关接口文档")
public class PaymentApiController extends BaseApiController {

    private final PaymentService paymentService;
    private final AllInPayPaymentService allInPayPaymentService;
    private final SupportPaymentService supportPaymentService;

    @Autowired
    public PaymentApiController(SupportPaymentService supportPaymentService, PaymentService paymentService, AllInPayPaymentService allInPayPaymentService) {
        this.paymentService = paymentService;
        this.allInPayPaymentService = allInPayPaymentService;
        this.supportPaymentService = supportPaymentService;
    }

    @ApiOperation("创建支付单")
    @PostMapping("createPayment")
    PaymentRespVo createPayment(@RequestBody PaymentReqVo reqVo) throws PaymentOrderIsExpiredException, PaymentOrderIsPaidException, PaymentException {

        return paymentService.createPayment(reqVo);
    }

    @ApiOperation("获取支付状态")
    @PostMapping("checkPaymentStatus")
    PaymentQueryRespVo checkPaymentStatus(@RequestBody PaymentQueryReqVo reqVo) {

        return paymentService.checkPaymentStatus(reqVo);
    }

    @ApiOperation("退款")
    @PostMapping("refund")
    PaymentRefundRespVo refund(@RequestBody PaymentRefundReqVo reqVo) throws PaymentException {

        return paymentService.refund(reqVo);
    }

    @ApiOperation("退款进度查询")
    @PostMapping("refundQuery")
    PaymentRefundRespVo refundQuery(@RequestBody PaymentRefundReqVo reqVo) throws PaymentException {

        return paymentService.refundQuery(reqVo);
    }


    @ApiOperation("关闭支付单")
    @PostMapping("closePayment")
    public void closePayment(@RequestBody PaymentQueryReqVo reqVo) throws PaymentOrderIsPaidException {

        paymentService.closePayment(reqVo);
    }

    @ApiOperation("关闭支付单")
    @PostMapping("cancelPayment")
    public PaymentCancelResp cancelPayment(@RequestBody PaymentCancelReqVo reqVo) {
        return paymentService.cancelPayment(reqVo);
    }

    @ApiOperation("第三方支付通知处理")
    @PostMapping("handlePaymentNotify")
    public void handlePaymentNotify(@RequestBody PaymentNotifyReqVo reqVo) throws SignIncorrectException {

        paymentService.handlePaymentNotify(reqVo);
    }

    @ApiOperation("处理通联支付结果通知")
    @PostMapping("handleAllInPayNotify")
    public String handleAllInPayNotify(@RequestBody AllInPayPaymentNotifyReqVo reqVo) {
        return paymentService.handleAllInPayNotify(reqVo);
    }

    @ApiOperation("处理通联H5支付结果通知")
    @PostMapping("handleAllInPayH5Notify")
    public void handleAllInPayH5Notify(@RequestBody AllInPayH5PayRespVo reqVo) {
        paymentService.handleAllInPayH5Notify(reqVo);
    }

    @ApiOperation("构造通联H5支付参数")
    @PostMapping("buildH5Pay")
    public AllInPayH5PayReqVo buildH5Pay(@RequestBody PaymentReqVo reqVo) {
        return allInPayPaymentService.buildH5Pay(reqVo);
    }

    @ApiOperation("同步可用支付方式")
    @PostMapping("syncSupportPayment")
    public void syncSupportPayment(List<SupportPaymentSyncReqVo> reqVos) {
        supportPaymentService.syncSupportPayment(reqVos);
    }

    @ApiOperation("获取支付方式")
    @PostMapping("findByCode")
    public SupPaymentRespVo findByCode(String code) {
        return supportPaymentService.findByCode(code);
    }
    @ApiModelProperty("获取可用支付方式")
    @GetMapping("findAvailable")
    public List<SupPaymentRespVo> findAvailable(){
        return supportPaymentService.findAvailable();
    }
}

